using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace CollegeShop.Infrastructure.Entities.Repository
{
    [Table("T_Financecycle")]
    public class T_Financecycle : IEntity<T_Financecycle>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int FinanceCycleID { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(8)]
        public string FinanceCycleYear { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(8)]
        public string FinanceCycleMonth { get; set; }

        public DateTime BeginDate { get; set; }

        public DateTime EndDate { get; set; }

        public int Days { get; set; }

        public int IsFinish { get; set; }

        public DateTime CarryForwardTime { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(16)]
        public string CarryForwardType { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string CarryForwordUser { get; set; }

        public override T_Financecycle Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.FinanceCycleID == id);
        }

        public override T_Financecycle Find(System.Linq.Expressions.Expression<Func<T_Financecycle, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Financecycle.FirstOrDefault(where);
                if (item != null)
                {
                    this.FinanceCycleID = item.FinanceCycleID;
                    this.FinanceCycleYear = item.FinanceCycleYear;
                    this.FinanceCycleMonth = item.FinanceCycleMonth;
                    this.BeginDate = item.BeginDate;
                    this.EndDate = item.EndDate;
                    this.Days = item.Days;
                    this.IsFinish = item.IsFinish;
                    this.CarryForwardTime = item.CarryForwardTime;
                    this.CarryForwardType = item.CarryForwardType;
                    this.CarryForwordUser = item.CarryForwordUser;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Financecycle, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Financecycle.Any(where);
            }
        }
    }
}
